#!/bin/bash
# JDK 1.8 install
# 安装包与脚本在同一个目录下
echo "start to install JDK 1.8.0_141 ..."

jvmpath=/usr/lib/jvm

# 如果不存在
if [ ! -d "$jvmpath" ]; then
    echo "正在创建$jvmpath目录"
    mkdir $jvmpath
    echo "目录$jvmpath创建成功"
fi

jdkfile=$(ls | grep jdk-*-linux-*.tar.gz)
jdkdirname="jdk1.8.0_141"

os_version=`uname -a`
echo $os_version
architecture="64"
echo "$os_version" | grep -q "$architecture"
if [ $? -eq 0 ]
    then
        echo "准备安装64位JDK"
        if [ ! -f "$jdkfile" ]; then
            wget http://source.hankchan.cn/jdk-8u141-linux-x64.tar.gz
        fi
    else
    echo "对不起，你的操作系统不是64位，无法找到32位的JDK"
fi

jdkfile=$(ls | grep jdk-*-linux-*.tar.gz)

if [ -f "$jdkfile" ]; then
    tar -zxvf $jdkfile -C /usr/lib/jvm/
    echo "安装JDK成功"
    echo "开始配置环境变量"
    #cp ~/.bashrc ~/.bashrc.bak
    # cat ~/.bashrc.bak >> ~/.bashrc
    # 对CentOS和Ubuntu通用
    update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/$jdkdirname/bin/java" 1
    update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/$jdkdirname/bin/javac" 1
    update-alternatives --set java /usr/lib/jvm/$jdkdirname/bin/java
    update-alternatives --set javac /usr/lib/jvm/$jdkdirname/bin/javac
    echo "$jdkfile install success"
    . /etc/profile
    echo "配置环境变量成功"
    java -version
    echo "安装成功"
fi
